<template>
  <div>
    <div class="h60">
      <span class="exnum">{{ Num }}.{{ item.title }}</span>
      <span class="labels">
        <span v-if="item.isJt == '1'">机经</span>
        <span v-if="item.isJt !== '1'" style="background: #7cc3d6">练习题</span>
        <span
          style="background: #ff5e4e"
          v-if="item.labelStyle == 'wrong-color'"
          >错题</span
        >
        <span style="background: #13bfbf" v-if="item.isYc == '1'"
          >本周预测</span
        >
        <span style="background: #94d0f7">{{ item.titleNumber }}</span>
      </span>
    </div>

    <!-- 录音播放区域 -->
    <div class="player">
      <Audioplayer
        v-if="item.url"
        :url="item.url"
        :readyTime="readyTime"
        :answerTime="answerTime"
        @sendTime="getTime"
        @startRecord="startRecord"
        ref="aplayer"
      ></Audioplayer>
       <h1 v-else class="none_audio_tip">此题暂未上传录音</h1>
    </div>
    <div class="time">
      <span>剩余时间：{{ s_to_hs(answerTime) }}</span>
    </div>

    <div class="sst">
      <el-form>
        <textarea
          @input="descInput"
          v-model="answer"
          class="content"
          placeholder="输入你的回答"
        ></textarea>
      </el-form>
      <span>单词数：{{ countNum }}</span>
    </div>
  </div>
</template>

<script>
import writingUtil from "@/utils/writingUtil";
import Audioplayer from '../../player'
export default {
  props: ['url', 'item', 'Num'],
  mixins: [writingUtil],
  components: {
    Audioplayer,
  },
  data () {
    return {
      readyTime: 3,//准备时间
      answerTime: 600,//答题时间
      answer: '',
      countNum: 0,
    }
  },
  created () {

    this.answer=this.item.answer
    if (this.answerTime > 0) {
      setInterval(() => {
        if(this.answerTime>0){
          this.answerTime--
        }
      }, 1000);
    }
  },
  methods: {

    // 录音播放完毕，自动开始录音
    startRecord () {
      console.log('end');
      var that = this
      this.answerTimeCount()
      this.$nextTick(() => {
        that.startRecorder()
      })

    },
    // 播放音频的时候
    getTime () {
      clearInterval(this.interval.answerTime)
      this.answerTime = 0
      this.readyTime = 0
    },
    // 点击麦克风
    handleClick () {
      // 获取了麦克风权限才允许点击麦克录音
      if (this.tips) {
        this.isOpen = !this.isOpen
        if (!this.isOpen) { // 开启录音
          this.Rurl = ''
          this.readyTime = 0
          clearInterval(this.interval.answerTime)
          if (this.answerTime !== 0) {
            this.answerTime = 0
            this.answerTimeCount()
          }
        } else { // 关闭录音
          this.getRecorder()
          clearInterval(this.interval.answerTime)
        }
      } else {
        this.$msg.info('请先打开麦克风授权')
      }
    },
    // 准备答题倒计时
    coutDown () {
      var that = this
      this.interval.mkf = setInterval(() => {
        if (this.readyTime > 0) {
          this.readyTime--
        } else {
          if (this.answerTime == 0) {
            return
          } else {
            this.isOpen = false
          }
        }
      }, 1000);
    },// 答题时间计时
    answerTimeCount () {
      clearInterval(this.interval.answerTime)
      this.answerTime = 0
      this.interval.answerTime = setInterval(() => {
        // 答题时间上限
        if (this.answerTime < 15) {
          this.answerTime++
        } else {
          this.isOpen = true
          this.getRecorder()
          clearInterval(this.interval.answerTime)
        }
      }, 1000);


    },
    // 输入字数
    descInput () {
      this.$emit('sendAnswer', this.answer)
      if (this.answer.split(' ').length > 200) {
        this.answer = this.answer.slice(0, 200)
        this.$msg.error("字数不能超过200")
      }
      this.countNum = this.answer.split(' ').length 
    }
  },

}
</script>

<style lang="less" scoped>

.none_audio_tip{
  font-size: 1.25rem;
  color: #999;
  padding: 1.125rem;
}
.player{
  margin-bottom: 1rem;
  border: 1px dashed #ff5e4e;
  background-color: #fffafa;
}
.time {
  font-size: 1rem;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #333333;
  position: relative;
  margin-top: 1.875rem;
  span {
    margin-right: 1.875rem;
  }
  .iconfont {
    font-size: 1.5rem;
    color: #ccc;
  }
  .end {
    position: absolute;
    right: -1.5rem;
    text-align: right;
  }
}
// .box {
//   margin: 1.25rem 0;
// }
//  听力总结
.sst {
  height: 20.8125rem;
  padding: 1.875rem 0rem;
  background-color: #fff;
  position: relative;
  .content {
    width: 100%;
    height: 17.0625rem;
    background-color: #f7f7f7;
    resize: none;
    border: none;
    outline: none;
    padding: 1.25rem;
    color: #999;
    font-size: 1.125rem;
  }
  span {
    position: absolute;
    right: 1.75rem;
    bottom: 2.5rem;
  }
}
.record {
  margin-left: 15rem;
}
.operate_mic {
  text-align: center;
  font-size: 1.25rem;
}
.setpink {
  background-color: #ff5e4e !important;
}
.microphone {
  width: 4.375rem;
  height: 4.375rem;
  line-height: 4.375rem;
  text-align: center;
  margin-left: 47%;
  border-radius: 50%;
  cursor: pointer;
  background-color: #eee;
  .iconfont {
    font-size: 2.5rem;
    color: #fff;
  }
  .countdown {
    height: 2.8125rem;
    line-height: 2.8125rem;
    font-size: 1rem;
    color: #333;
    text-align: center;
  }
}
.h60 {
  height: 3.75rem;
  line-height: 3.75rem;
  padding-left: 1.25rem;
  box-sizing: border-box;
  background-color: #f7f7f7;
  display: flex;
  position: relative;
  margin-bottom: 1rem;
  .exnum {
    width: 45rem;
    font-size: 1rem;
    color: #666;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .labels {
    display: flex;
    justify-content: flex-end;
    width: 21.25rem;
    span {
      height: 1.875rem;
      line-height: 1.875rem;
      padding: 0 0.375rem;
      box-sizing: border-box;
      background-color: #ff9b4f;
      border-radius: 0.1875rem;
      margin: 0.9375rem 0.46875rem;
      color: #fff;
      box-sizing: border-box;
    }
  }
}
</style>